<?php

namespace app\ask\controller;

use think\Controller;

class Friend extends Controller
{
    protected $FriendModel;
    protected $BusinessModel;

    public function __construct()
    {
        parent::__construct();

        $this->FriendModel = model('Friend');
        $this->BusinessModel = model('Business.Business');
    }

    public function index()
    {
        $page = $this->request->param('page', 1, 'trim');
        $limit = $this->request->param('limit', 0, 'trim');
        $busid = $this->request->param('busid', 0, 'trim');
        $keyword = $this->request->param('keyword', '', 'trim');
        $action = $this->request->param('action', 'focus', 'trim');

        if ($action !== 'focus' && $action !== 'fans') {
            $this->error('参数错误');
            exit;
        }

        if (!$limit) {
            $limit = 10;
        }

        // 分页偏移量
        $offset = ($page - 1) * $limit;

        $where = [];

        if ($action == 'focus') {
            $where['fa_friend.busid'] = $busid;
        } else {
            $where['fansid'] = $busid;
        }

        $total = $this->FriendModel
            ->where($where)
            ->count();

        if ($keyword) {
            $where['fa_business.nickname'] = ['like', '%' . $keyword . '%'];
        }

        $list = $this->FriendModel
            ->with([$action])
            ->where($where)
            ->order('id', 'desc')
            ->limit($offset, $limit)
            ->select();

        if (!empty($list)) {
            foreach ($list as &$item) {
                $item = $item->toArray();

                $field = ['id', 'avatar_text', 'nickname'];

                if (isset($item['focus'])) {
                    $item['business'] = filter_property($item['focus'], $field);
                    $item['focus'] = $item['business'];
                    // unset($item['focus']);
                }
                if (isset($item['fans'])) {
                    $item['business'] = filter_property($item['fans'], $field);
                    $item['fans'] = $item['business'];
                    // unset($item['fans']);
                }
            }
        }

        // myPrinf($list[0]);exit;

        $this->success('返回数据', null, ['list' => $list ?? [], 'total' => $total]);
    }

    // 添加关注
    public function add()
    {
        $busid = $this->request->param('busid', 0, 'intval');
        $trackerid = $this->request->param('trackerid', 0, 'intval'); // 被关注者id

        if ($busid === $trackerid) {
            $this->error('不能关注自己');
            exit;
        }

        $bus = $this->BusinessModel->find($busid);

        if (!$bus) {
            $this->error('用户不存在');
            exit;
        }

        $tracker = $this->BusinessModel->find($trackerid);

        if (!$tracker) {
            $this->error('该用户不存在');
            exit;
        }

        $friend = $this->FriendModel->where(['busid' => $busid, 'fansid' => $trackerid])->find();

        if ($friend) {
            $this->error('您已经关注过了');
            exit;
        }

        $data = [
            'busid' => $busid,
            'fansid' => $trackerid,
        ];

        $friendRes = $this->FriendModel->save($data);

        if ($friendRes === false) {
            $this->error('关注失败');
            exit;
        } else {
            $this->success('关注成功');
            exit;
        }
    }

    // 取消关注
    public function cancel()
    {
        $busid = $this->request->param('busid', 0, 'intval');
        $trackerid = $this->request->param('trackerid', 0, 'intval'); // 被关注者id

        if ($busid === $trackerid) {
            $this->error('不能取消关注自己');
            exit;
        }

        $bus = $this->BusinessModel->find($busid);

        if (!$bus) {
            $this->error('用户不存在');
            exit;
        }

        $tracker = $this->BusinessModel->find($trackerid);

        if (!$tracker) {
            $this->error('该用户不存在');
            exit;
        }

        $friend = $this->FriendModel->where(['busid' => $busid, 'fansid' => $trackerid])->find();

        if (!$friend) {
            $this->error('没有找到关注记录');
            exit;
        }

        $friendRes = $this->FriendModel->where(['id' => $friend['id']])->delete();

        if ($friendRes === false) {
            $this->error('取消关注失败');
            exit;
        } else {
            $this->success('取消关注成功');
            exit;
        }
    }

    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        $busid = $this->request->param('busid', 0, 'intval');
        $actionid = $this->request->param('actionid', 0, 'intval');

        $friend = $this->FriendModel->find($id);

        if (!$friend) {
            $this->error('没有找到关注记录');
            exit;
        }

        $bus = $this->BusinessModel->find($busid);

        if (!$bus) {
            $this->error('用户不存在');
            exit;
        }

        $actionBus = $this->BusinessModel->find($actionid);

        if (!$actionBus) {
            $this->error('操作用户不存在');
            exit;
        }

        $msgPrefix = '';

        if ($bus['id'] !== $friend['busid'] && $actionBus['id'] !== $friend['fansid']) {
            $msgPrefix = '删除';
        } else if ($bus['id'] !== $friend['fansid'] && $actionBus['id'] !== $friend['busid']) {
            $msgPrefix = '取消关注';
        } else {
            $this->error('非法操作');
            exit;
        }

        $res = $this->FriendModel->where(['id' => $id])->delete();

        if ($res === false) {
            $this->error($msgPrefix . '失败');
            exit;
        }

        $this->success($msgPrefix . '成功');
        exit;
    }
}
